package com.smec.remes.module.submit.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.acker.simplezxing.activity.CaptureActivity;
import com.google.gson.Gson;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.smec.moblie.R;
import com.smec.moblie.SMECApplication;
import com.smec.moblie.activity.ArrayListStock;
import com.smec.moblie.activity.LoginActivity;
import com.smec.moblie.databinding.ActivityWeishouhuoBinding;
import com.smec.remes.base.SmecBaseActivity;
import com.smec.remes.bean.NewPackingList;
import com.smec.remes.bean.SubmitDetails;
import com.smec.remes.bean.SubmitDetailsConfirme;
import com.smec.remes.bean.SubmitSure;
import com.smec.remes.bean.SubmitSureList;
import com.smec.remes.bean.query.Packing;
import com.smec.remes.bean.query.Stock;
import com.smec.remes.module.search.SmecSearchActivity;
import com.smec.remes.module.submit.SmecSubmitPresenter;
import com.smec.remes.module.submit.adapter.SmecWeiChildrenAdapter;
import com.smec.remes.smecManager.SmecRetrofit.HttpResult;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.GetFullUrl;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by Administrator on 2017/12/22.
 */

public class SubmitWeiShouHuoActivity extends SmecBaseActivity<SmecSubmitPresenter> {

    private ActivityWeishouhuoBinding activityWeishouhuoBinding;
    private SmecWeiChildrenAdapter smecWeiChildrenAdapter;

    public static final String W_DETAILS="W_DETAILS";
    public static final String W_DETAILSFAIL="W_DETAILSFAIL";
    public static final String SHOUHUO="SHOUHUO";
    public static final String SHOUHUOFAIL="SHOUHUOFAIL";
    private boolean data=true;
    private String applyOrderNo="";
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityWeishouhuoBinding=DataBindingUtil.setContentView(this, R.layout.activity_weishouhuo);
        smecWeiChildrenAdapter=new SmecWeiChildrenAdapter(this);
        activityWeishouhuoBinding.weishouhuoRecyclerView.setLayoutManager(new LinearLayoutManager(SubmitWeiShouHuoActivity.this));
        activityWeishouhuoBinding.weishouhuoRecyclerView.setAdapter(smecWeiChildrenAdapter);
        _return();
        _getIntent();
    }


    public void _getIntent(){
        Intent intent=getIntent();
        applyOrderNo=intent.getStringExtra("applyOrderNo");
        mPresenter._weishouhuoDetails(applyOrderNo);
    }
    public void _return(){
        activityWeishouhuoBinding.searchTopbar.getTvTextLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                mRxBus.post("Refresh","true");
            }
        });
        activityWeishouhuoBinding.searchTopbar.gettvTextRightSecond().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(SubmitWeiShouHuoActivity.this, CaptureActivity.class), CaptureActivity.REQ_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CaptureActivity.REQ_CODE:
                switch (resultCode) {
                    case RESULT_OK:
                        if(!TextUtils.isEmpty(data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT))){
                            Log.w("aa",data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT));
                            String str=data.getStringExtra(CaptureActivity.EXTRA_SCAN_RESULT);
                            String confirmNo=str;
                            Intent intent=new Intent(SubmitWeiShouHuoActivity.this,SmecPackingActivity.class);
                            intent.putExtra("confirmNo",confirmNo);
                            startActivity(intent);
                        }
                        break;
                    case RESULT_CANCELED:
                        if (data != null) {
                            Log.e("SSS","ssss");
                        }
                        break;
                }
                break;
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(W_DETAILS)
            }
    )

    public void _detailsSuccess(ArrayList<SubmitDetails> arrayList){
        if(CommonUtils.notEmpty(arrayList)){
            if(CommonUtils.notEmpty(arrayList.get(0).getTobeconfirmList())){
                smecWeiChildrenAdapter.setArrayList(arrayList.get(0).getTobeconfirmList());
            }
        }else {
            data=false;
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(W_DETAILSFAIL)
            }
    )

    public void _detailsFail(String a){
        activityWeishouhuoBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SHOUHUO)
            }
    )

    public void _shouhuo(HttpResult httpResult){
       activityWeishouhuoBinding.getRoot().postDelayed(new Runnable() {
           @Override
           public void run() {
               progressDialog.dismiss();
               Toast.makeText(SubmitWeiShouHuoActivity.this,"确认收货成功",Toast.LENGTH_SHORT).show();
               finish();
           }
       },700);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SHOUHUOFAIL)
            }
    )

    public void _shouhuoFail(String a){

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter._weishouhuoDetails(applyOrderNo);
    }

    @Override
    public SmecSubmitPresenter getPresenter() {
        return new SmecSubmitPresenter(this);
    }
}
